package com.hongxun.design.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by zerdoor_pc on 2016/4/7.
 */
public class TimeUtil {
    public static String time(String time) {
        //MyLog.L(MString.SUCCESSED+" time="+time);
        Date newsDate = new Date(Long.parseLong(time));
        Date nowDate = new Date(System.currentTimeMillis());
        long betweenTime = nowDate.getTime() - newsDate.getTime();
        int minute = (int) betweenTime / 1000 / 60;

        SimpleDateFormat sf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newsDate2 = sf4.format(newsDate);
        //MyLog.L(MString.SUCCESSED+" newsDate2="+newsDate2);
        return newsDate2;
    }

    public static String getyyyyMMdd(String time) {
        if (time == null || time.length() == 0) return "";
        Date date = new Date(Long.parseLong(time));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return formatter.format(date);
    }

    public static String time(String time, String style) {
        Date newsDate = new Date(Long.parseLong(time));
        Date nowDate = new Date(System.currentTimeMillis());
        long betweenTime = nowDate.getTime() - newsDate.getTime();
        int minute = (int) betweenTime / 1000 / 60;
        SimpleDateFormat sf4 = new SimpleDateFormat(style);
        String newsDate2 = sf4.format(newsDate);
        return newsDate2;
    }

    public static String time(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String timeSFM(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String getTime(String time) {
        Date newsDate = new Date(Long.parseLong(time));
        Date nowDate = new Date(System.currentTimeMillis());
        long betweenTime = nowDate.getTime() - newsDate.getTime();
        SimpleDateFormat sf3 = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sf4 = new SimpleDateFormat("yyyy-MM-dd");
        int minute = (int) (betweenTime / 1000 / 60);
        if (minute <= 1) {
            return "刚刚";
        } else if (minute < 60) {
            return String.valueOf(minute) + "分钟前";
        } else if (minute < 1440) {
            return sf3.format(newsDate);
//            return String.valueOf(minute/60)+"小时前";
        } else {
            String newsDate2 = sf4.format(newsDate);
            return newsDate2;
        }

    }

    public static String getTime() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(System.currentTimeMillis());
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH)+1;
//        int day = calendar.get(Calendar.DAY_OF_MONTH);
//        int hours = calendar.get(Calendar.HOUR_OF_DAY);
//        int minute = calendar.get(Calendar.MINUTE);
//        return year+"-"+month+"-"+day;
        Date nowDate = new Date(System.currentTimeMillis());
        SimpleDateFormat sf4 = new SimpleDateFormat("yyyy-MM-dd");
        String newsDate2 = sf4.format(nowDate);
        //MyLog.L(MString.SUCCESSED+" newsDate2="+newsDate2);
        return newsDate2;

    }


    public static int getYear(String mills) {
        Date nowDate = new Date(Long.parseLong(mills));
        SimpleDateFormat sf4 = new SimpleDateFormat("yyyy");
        String newsDate2 = sf4.format(nowDate);
        //MyLog.L(MString.SUCCESSED+" newsDate2="+newsDate2);
        return Integer.parseInt(newsDate2);
    }

    public static long stringTolong(String strTime, String formatType) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String longToStr(long time) {
        if (String.valueOf(time).length() == 10)
            return time(new Date(Long.valueOf(String.valueOf(time) + "000")));
        else
            return time(new Date(time));
    }
}
